import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  CrudExpose,
} from '@fast-crud/fast-crud';

import type { FormRulesExt } from '#/api';
import type { DefClientModel } from '#/api/devOperation/system/model/defClientModel';
import type { ActionEnum } from '#/enums/commonEnum';

import { ref } from 'vue';

import { useAccess } from '@vben/access';
import { $t } from '@vben/locales';

import { DefClientApi } from '#/api/devOperation/system/defClient';
import { DictEnum } from '#/enums/commonEnum';
import { PermCode } from '#/enums/perm';
import {
  backendDict,
  createdTimeColumn,
  deleteButton,
  indexColumn,
  YES_NO_CONSTANT_DICT,
} from '#/plugins/fast-crud/common';

const { hasPermission } = useAccess();
const permCode = PermCode.devOperation.system.defClient;

export function createCrudOptions(
  props: CreateCrudOptionsProps<DefClientModel.DefClientResultVO, any>,
): CreateCrudOptionsRet<DefClientModel.DefClientResultVO> {
  const selectedRowKeys = ref<string[]>([]);

  return {
    crudOptions: {
      request: {
        pageRequest: DefClientApi.pageRequest,
        addRequest: DefClientApi.addRequest,
        editRequest: DefClientApi.editRequest,
        delRequest: DefClientApi.delRequest,
        infoRequest: DefClientApi.infoRequest,
      },
      actionbar: {
        buttons: {
          add: { show: hasPermission(permCode.add) },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            role: permCode.delete,
            removeFn: DefClientApi.remove,
          }),
        },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: '200px',
        buttons: {
          edit: { show: hasPermission(permCode.edit) },
          remove: { show: hasPermission(permCode.delete) },
          copy: { show: hasPermission(permCode.add) },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        clientId: {
          title: $t('devOperation.system.defClient.clientId'),
          type: 'text',
          search: { show: true },
          viewForm: { show: true },
          form: { show: false },
        },
        clientSecret: {
          title: $t('devOperation.system.defClient.clientSecret'),
          type: 'text',
          viewForm: { show: true },
          form: { show: false },
          column: { show: false },
        },
        name: {
          title: $t('devOperation.system.defClient.name'),
          type: 'text',
          search: { show: true },
        },
        type: {
          title: $t('devOperation.system.defClient.type'),
          type: 'dict-radio',
          dict: backendDict(DictEnum.CLIENT_TYPE),
        },
        remarks: {
          title: $t('devOperation.system.defClient.remarks'),
          type: 'textarea',
          column: { show: false },
        },
        state: {
          title: $t('devOperation.system.defClient.state'),
          type: 'dict-radio',
          dict: YES_NO_CONSTANT_DICT,
          search: { show: true },
          form: { value: true },
        },
        ...createdTimeColumn({}),
      },
    },
  };
}

export const frontRules = (
  _crudExpose: CrudExpose,
  _mode: ActionEnum,
): FormRulesExt => {
  return {};
};
